<template>
  <div ref="chart" class="chart-container"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  data() {
    return {
      chartData: [
        { value: 30, name: '兰花爱好者' },
        { value: 20, name: '玫瑰花爱好者'},
        { value: 15, name: '郁金香爱好者' },
        { value: 35, name: '牡丹花爱好者' },
      ],
    };
  },
  mounted() {
    // 初始化图表
    const chart = echarts.init(this.$refs.chart);

    // 自定义颜色数组
    const colors = ['#ee3f36', '#fbd800', '#61bc5b', '#006de2'];

    const option = {
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)',
      },
      legend: {
        orient: 'vertical',
        left: 10,
        data: this.chartData.map(item => item.name),
      },
      series: [
        {
          name: '用户群体比例',
          type: 'pie',
          radius: ['50%', '70%'],
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: 'center',
          },
          emphasis: {
            label: {
              show: false, // 鼠标悬停时的文本隐藏
              fontSize: 14,
              fontWeight: 'bold',
            },
          },
          labelLine: {
            show: false,
          },
          data: this.chartData,
        },
      ],
      graphic: [
        {
          type: 'text',
          left: 'center',
          top: 'center',
          style: {
            text: '用户群体比例',
            textAlign: 'center',
            fill: '#000',
            fontSize: 20,
            fontWeight: 'bold',
          },
        },
      ],
    };

    chart.setOption(option);
  },
};
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 500px;
}
</style>
